#include <QCoreApplication>
#include <QMessageBox>
#include <QQmlContext>

#include "MApplication.h"
#include "RunGuard.h"

int main(int argc, char *argv[])
{
    RunGuard gurad("MPlayerRunGuardKey");
    if (!gurad.tryToRun()) {
        QApplication errApp(argc, argv);
        QMessageBox::critical(nullptr,
                              QObject::tr("Error"),
                              QObject::tr("A second instance of %1 is already running. Please close the other instance and try again.").arg("MPlayer")
                              );
        return -1;
    }

    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

    MApplication* app = new MApplication(argc, argv);
    Q_CHECK_PTR(app);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("appDir", qApp->applicationDirPath());

    app->_initCommon();
    if (!app->_initForNormalAppBoot()) {
        return -1;
    }
    int exitCode = 0;
    exitCode = app->exec();

    app->_shutdown();
    app->deleteLater();
    return exitCode;
}
